import { NextFunction ,Request , Response} from "express";
import { IGetMockDataResult } from "./getMockData";
import matchMock from "./matchMock";

export default function createMiddleware(opts = {} as IGetMockDataResult){

    const {mockData} = opts
    return {
        middleware:(req: Request, res: Response, next: NextFunction)=>{
            const match = mockData && matchMock(req, mockData);
            if (match) {
                console.log(`mock matched: [${match.method}] ${match.path}`);
                return match.handler(req, res, next);
              } else {
                return next();
              }
        }
    }
}